package sunwin.yoms.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.domain.system.BlogrollDomain;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.resolverArg.YomsArrayParam;
import sunwin.yoms.web.comm.YomsAjaxAnswer;
import sunwin.yoms.web.comm.YomsController;

import java.util.List;

/**
 * Created by watson on 2015/8/17.
 */
@Controller
public class BlogrollController {
    @Autowired
    private sunwin.yoms.service.system.BlogrollService blogrollService;
    private final String FindBvm = "system/blogroll.find";
    private final String CreateBvm = "system/blogroll.create";
    private final String EditBvm = "system/blogroll.edit";

    //---------------------赛为智能-------------------
    //查找友情链接
    @RequestMapping(value = R.FindBlogroll, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindBlogroll, viewName = FindBvm)
    public ModelAndView findBlogroll(BlogrollDomain blogrollDomain, ModelMap modelMap) throws YomsException {
        modelMap.addAttribute("blogrolls", blogrollDomain);
        List<BlogrollDomain> blogrollDomains = blogrollService.findBrogroll(blogrollDomain);
        ModelAndView mav = new ModelAndView();
        mav.addObject("blogrollsList", blogrollDomains);
        return mav;
    }

    //上移
    @RequestMapping(value = R.BlogrollUp, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.BlogrollUp)
    @ResponseBody
    public YomsAjaxAnswer up(int id) throws YomsException {
        blogrollService.Up(id);
        YomsAjaxAnswer ajax = YomsAjaxAnswer.getInstance();
        return ajax;
    }

    //下移
    @RequestMapping(value = R.BlogrollDown, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.BlogrollDown)
    @ResponseBody
    public YomsAjaxAnswer down(int id) throws YomsException {
        blogrollService.Down(id);
        YomsAjaxAnswer ajax = YomsAjaxAnswer.getInstance();
        return ajax;
    }

    //编辑访问页面
    @RequestMapping(value = R.EditBlogroll, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.EditBlogroll, viewName = EditBvm)
    public ModelAndView editBlogroll(int id) throws YomsException {
        BlogrollDomain blogrollDomain = blogrollService.getBrogrollById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("blogroll", blogrollDomain);
        return mav;
    }

    //编辑友情链接
    @RequestMapping(value = R.EditBlogroll, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.EditBlogroll, viewName = EditBvm, isAjax = true)
    @ResponseBody
    public YomsAjaxAnswer editBlogroll(BlogrollDomain blogrollDomain) throws YomsException {
        blogrollService.updateBlogroll(blogrollDomain);
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

    //创建友情链接访问页面
    @RequestMapping(value = R.CreateBlogroll, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.CreateBlogroll, viewName = CreateBvm)
    public void createBlogroll() throws YomsException {
    }

    //创建友情链接
    @RequestMapping(value = R.CreateBlogroll, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.CreateBlogroll, viewName = CreateBvm)
    @ResponseBody
    public YomsAjaxAnswer createBlogroll(BlogrollDomain blogrollDomain) throws YomsException {
        blogrollService.createBrogroll(blogrollDomain);
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;

    }

    //删除友情链接
    @RequestMapping(value = R.DelBlogroll, method = RequestMethod.POST)
    @YomsController(functionNo = YomsFunction.DelBlogroll)
    @ResponseBody
    public YomsAjaxAnswer delBlogroll(@YomsArrayParam List<Integer> ids) throws YomsException {
        if (ids != null) {
            for (int id : ids) {
                blogrollService.delBrogroll(id);
            }
        }
        YomsAjaxAnswer yomsAjaxAnswer = new YomsAjaxAnswer();
        yomsAjaxAnswer.setSuccess(true);
        return yomsAjaxAnswer;
    }

}
